package com.shaoshaohuo.app.ui.shipper;

import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.FinishPayOrderDetailsAdapter;
import com.shaoshaohuo.app.entity.FragmentNewCarEntity;

import java.util.List;

public class FinishPayOrderDetailsActivity extends AppCompatActivity implements View.OnClickListener {
    /**
     * tobar
     */
    private ImageView order_tobar_back;
    private TextView order_tobar_enter;
    private ImageView order_tobar_right;
    private LinearLayout order_layout_topbar_left;

    private ListView finish_pay_Order_details_listView;
    private FinishPayOrderDetailsAdapter adapter;
    private List<FragmentNewCarEntity.DataBean> sure_order_shop_data;
    private String orderid;
    private String time;
    private String totalmoney;
    private TextView finish_pay_Order_details_bianhao;
    private TextView finish_pay_Order_details_pay_moner_hour;

    private TextView finish_pay_Order_details_heji;
    private TextView finish_pay_Order_details_all_money;

    private LinearLayout finish_pay_Order_details_find_kefu;
    private TextView finish_pay_Order_details_copy;

    private void getIntentData() {
        Intent intent = getIntent();
        if (intent != null) {
            Bundle extras = intent.getExtras();
            sure_order_shop_data = (List<FragmentNewCarEntity.DataBean>) extras.getSerializable("shop_data");
            orderid = extras.getString("orderid");
            time = extras.getString("time");
            totalmoney = extras.getString("totalmoney");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finish_pay_order_details);
        getIntentData();
        initView();
        setView();
        bindAdapter();
        initListener();
    }

    private void initListener() {
        order_tobar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        finish_pay_Order_details_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager copy_word = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                String copy_trim = finish_pay_Order_details_bianhao.getText().toString().trim();
                ClipData clipData = ClipData.newPlainText("Label", copy_trim);
                copy_word.setPrimaryClip(clipData);
                if (copy_word.getPrimaryClip().toString().trim() != null) {
                    Toast.makeText(getApplicationContext(), "复制成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
        finish_pay_Order_details_find_kefu.setOnClickListener(this);
    }

    private void bindAdapter() {
        finish_pay_Order_details_listView.setAdapter(adapter);
    }

    private void setView() {
        finish_pay_Order_details_pay_moner_hour.setText(time);
        finish_pay_Order_details_bianhao.setText(orderid);
        finish_pay_Order_details_heji.setText(totalmoney);
        finish_pay_Order_details_all_money.setText(totalmoney);
    }

    private void initView() {
        /**
         * tobar
         * */
        order_tobar_back = (ImageView) findViewById(R.id.imageview_topbar_left_backImage);
        order_tobar_back.setVisibility(View.VISIBLE);
        order_tobar_back.setImageResource(R.mipmap.return_key);
        order_tobar_enter = (TextView) findViewById(R.id.toolbar_topname);
        order_tobar_enter.setText("订单详情");
        order_tobar_right = (ImageView) findViewById(R.id.imageview_message);
        order_tobar_right.setVisibility(View.GONE);
        order_layout_topbar_left = (LinearLayout) findViewById(R.id.layout_topbar_left);
        order_layout_topbar_left.setVisibility(View.VISIBLE);

        finish_pay_Order_details_listView = (ListView) findViewById(R.id.finish_pay_Order_details_listView);
        adapter = new FinishPayOrderDetailsAdapter(this, sure_order_shop_data);
        finish_pay_Order_details_bianhao = (TextView) findViewById(R.id.finish_pay_Order_details_bianhao);
        finish_pay_Order_details_pay_moner_hour = (TextView) findViewById(R.id.finish_pay_Order_details_pay_moner_hour);

        finish_pay_Order_details_find_kefu = (LinearLayout) findViewById(R.id.finish_pay_Order_details_find_kefu);
        finish_pay_Order_details_copy = (TextView) findViewById(R.id.finish_pay_Order_details_copy);
        finish_pay_Order_details_all_money = (TextView) findViewById(R.id.finish_pay_Order_details_all_money);
        finish_pay_Order_details_heji = (TextView) findViewById(R.id.finish_pay_Order_details_heji);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish_pay_Order_details_find_kefu:
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:10086"));
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
        }
    }
}
